package com.bite.forum.service.impl;


import com.bite.forum.common.AppResult;
import com.bite.forum.common.ResultCode;
import com.bite.forum.dao.MessageMapper;
import com.bite.forum.exception.ApplicationException;
import com.bite.forum.model.Message;
import com.bite.forum.model.User;
import com.bite.forum.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageService implements IMessageService {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private MessageMapper messageMapper;

    public void create(Message message){
        if(message == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        User receiveUser = userService.selectByPrimaryKey(message.getReceiveUserId());
        if(receiveUser == null){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        message.setState((byte)0);
        message.setDeleteState((byte)0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        int row = messageMapper.insertSelective(message);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Message selectById(Long id) {
        if(id == null || id < 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        if(message == null){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        return message;
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if(receiveUserId == null || receiveUserId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }

        Integer count = messageMapper.selectUnreadCount(receiveUserId);

        if(count == null){
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId == null || receiveUserId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        // 非空校验, state : 0 未读 1 已读 2 已回复
        if (id == null || id <= 0 || state < 0 || state > 2) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Message message = new Message();
        message.setState(state);
        message.setId(id);
        Date date = new Date();
        message.setUpdateTime(date);

        int row = messageMapper.updateByPrimaryKeySelective(message);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if(repliedId == null || repliedId <= 0 || message == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existMessfge = messageMapper.selectByPrimaryKey(repliedId);
        if(existMessfge.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.getMessage());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }

        updateStateById(repliedId , (byte)2);
        create(message);
    }
}



























